// pages/topic-shengren-end/topic-shengren-end.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    countdownTime: 60, // 倒计时时间
  },

  onReady(){
    // 页面进入倒计时
    this.startCountdown();
  },
  // 倒计时
  startCountdown() {
    // 清除已有倒计时
    if (this.data.countdownInterval) {
      clearInterval(this.data.countdownInterval);
    }
    this.setData({
      countdownTime: 60
    });
    const interval = setInterval(() => {
      let newTime = this.data.countdownTime - 1;
      if (newTime > 0) {
        this.setData({
          countdownTime: newTime
        });
      } else {
        clearInterval(interval);
        // 倒计时结束，进入下一步
        wx.showToast({
          title: '进入下一步',
          icon: 'none',
          duration: 1000
        });
        setTimeout(() => {
          this.onStar();
        }, 1000);
      }
    }, 1000);
    this.setData({
      countdownInterval: interval
    });
  },
  onStar(){
    // 清除倒计时
    if (this.data.countdownInterval) {
      clearInterval(this.data.countdownInterval);
      this.setData({ countdownInterval: null });
    }
    wx.reLaunch({
      url: '../topic-another/topic-another?type=2',
    })
  },
  onUnload() {
    // 清除定时器
    if (this.data.countdownInterval) {
      clearInterval(this.data.countdownInterval);
      this.setData({ countdownInterval: null });
    }
  }
})